package org.feng.controller;

import org.feng.common.RequestHolder;
import org.feng.common.ResponseEntity;
import org.feng.common.util.GsonUtil;
import org.feng.service.IUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 表格控制器
 *
 * @author fengjinsong
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Resource
    private IUserService userService;

    @PostMapping(value = "/exportUser")
    public void exportUser() throws IOException {
        try {
            userService.exportUser();
        } catch (Exception e) {
            HttpServletResponse response = RequestHolder.RESPONSE_LOCAL.get();
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(GsonUtil.toJsonWithNull(new ResponseEntity<>("下载文件失败", "200", null)));
        }
    }
}
